package channel

import (
	"context"
	"errors"

	schema "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema/tenant/channel"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type TenantChannelWavepayModel struct {
	ctx context.Context
	db  *gorm.DB
}

func NewTenantChannelWavepayModel(ctx context.Context, db *gorm.DB) *TenantChannelWavepayModel {
	return &TenantChannelWavepayModel{
		ctx: ctx,
		db:  db,
	}
}

func (model *TenantChannelWavepayModel) getDb() *gorm.DB {
	if model.db != nil {
		return model.db
	}

	return svc.Db
}

func (model *TenantChannelWavepayModel) CreateWavepay(wavepay *schema.TenantChannelWavepay) error {
	res := model.getDb().Model(&schema.TenantChannelWavepay{}).Create(wavepay)
	if err := res.Error; err != nil {
		logx.Error(model.ctx, "tenantChannel-CreateWavepay,err", err)
		return err
	}
	return nil
}

func (model *TenantChannelWavepayModel) GetWavepayById(id int64) (*schema.TenantChannelWavepay, error) {
	var wavepay schema.TenantChannelWavepay
	res := model.getDb().Model(&schema.TenantChannelWavepay{}).Where("id = ?", id).First(&wavepay)
	if err := res.Error; err != nil {
		logx.Error(model.ctx, "tenantChannel-GetWavepayById,err", err)
		return nil, err
	}
	return &wavepay, nil
}

func (model *TenantChannelWavepayModel) GetWavepayByCode(code string) ([]schema.TenantChannelWavepay, error) {
	var wavepayList []schema.TenantChannelWavepay
	res := model.getDb().Model(&schema.TenantChannelWavepay{}).Where("code = ?", code).Find(&wavepayList)
	if err := res.Error; err != nil {
		logx.Error(model.ctx, "tenantChannel-GetWavepayByCode,err", err)
		return nil, err
	}
	return wavepayList, nil
}

func (model *TenantChannelWavepayModel) UpdateWavepay(wavepay *schema.TenantChannelWavepay) error {
	res := model.getDb().Model(&schema.TenantChannelWavepay{}).Where("id = ?", wavepay.ID).Updates(wavepay)
	if err := res.Error; err != nil {
		logx.Error(model.ctx, "tenantChannel-UpdateWavepay,err", err)
		return err
	}
	return nil
}

func (model *TenantChannelWavepayModel) DeleteWavepay(id int64) error {
	res := model.getDb().Model(&schema.TenantChannelWavepay{}).Where("id = ?", id).Delete(&schema.TenantChannelWavepay{})
	if err := res.Error; err != nil {
		logx.Error(model.ctx, "tenantChannel-DeleteWavepay,err", err)
		return err
	}
	return nil
}

func (model *TenantChannelWavepayModel) GetAllWavepay() ([]schema.TenantChannelWavepay, error) {
	var wavepayList []schema.TenantChannelWavepay
	res := model.getDb().Model(&schema.TenantChannelWavepay{}).Find(&wavepayList)
	if err := res.Error; err != nil {
		logx.Error(model.ctx, "tenantChannel-GetAllWavepay,err", err)
		return nil, err
	}
	return wavepayList, nil
}

func (model *TenantChannelWavepayModel) GetWavepayIsLimit() (int32, error) {
	var wavepay schema.TenantChannelWavepay
	res := model.getDb().Model(&schema.TenantChannelWavepay{}).First(&wavepay)
	if err := res.Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
		logx.Error(model.ctx, "tenantChannel-GetWavepayIsLimit,err", err)
		return 0, err
	}
	return wavepay.IsLimit, nil
}

func (model *TenantChannelWavepayModel) UpdateWavepayIsLimit(isLimit int32) error {
	updates := schema.TenantChannelWavepay{
		IsLimit: isLimit,
	}
	var total int64
	model.getDb().Model(&schema.TenantChannelWavepay{}).Count(&total)
	if total == 0 {
		return nil
	}
	res := model.getDb().Model(&schema.TenantChannelWavepay{}).Where("id>0").Updates(updates)
	if err := res.Error; err != nil {
		logx.Error(model.ctx, "tenantChannel-UpdateWavepayIsLimit,err", err)
		return err
	}
	return nil
}
